
function editGuardianName(id){  
    var html="<table><tr><td>First Name</td><td><input type='text' id='gfirstname' value='"+jQuery('#guardianfname'+id).val()+"'/></td></tr>";
    html+="<tr><td>Middle Name</td><td><input type='text' id='gmiddlename' value='"+jQuery('#guardianmname'+id).val()+"'/></td></tr>";
    html+="<tr><td>Last Name</td><td><input type='text' id='glastname' value='"+jQuery('#guardianlname'+id).val()+"'/></td></tr>";
    html+="<tr><td><input type='button' onclick='saveGuardianName()' value='Save Guardian'/></td></tr></table><input type='hidden' id='guardianid' value='"+id+"'>";
    jQuery("#name"+id).html(html);
}
function saveGuardianName(){
    var guardianid=jQuery('#guardianid').val();
    var firstname=jQuery('#gfirstname').val();
    var lastname=jQuery('#glastname').val();
    var middlename=jQuery('#gmiddlename').val();
    if(isNull(firstname)&&isNull(lastname)&&isNull(middlename)){
        jQuery("#editnameerror").html("Name cannot be null");
        return false;
    }
    var url='childupdate/updateguardian?firstname='+firstname+'&lastname='+lastname+'&middlename='+middlename+'&guardianid='+guardianid;
    jQuery.get(url,function(data){
        if(data){
            var names=data["names"];
            var name=names[0];
            alert(name.firstname);
        }else{
            alert('nod');
        }
    });
}
function  isNull(string){
    if(string==''){
        return true;
    }else
        return false;
}
function addGuardian(){
    var html="<table><tr><td>First Name</td><td><input type='text' id='gaddfirstname' value=''/></td></tr>";
    html+="<tr><td>Middle Name</td><td><input type='text' id='gaddmiddlename' value=''/></td></tr>";
    html+="<tr><td>Last Name</td><td><input type='text' id='gaddlastname' value=''/></td></tr>";
    html+="<tr><td>Phone Number</td><td><input type='text' id='gaddphonenumber' value=''/></td></tr>";
    var selectHtml='';
    jQuery.get("relationshiptype/getallrelationshiptypes",function(data){
        if(data){
            var relationshiptypes=data["relationshiptypes"];
            selectHtml+="<tr><td>Relationship</td><td><select id='gaddrelationship'>";
            for(var i=0;i<relationshiptypes.length;i++){
                var relationshiptype=relationshiptypes[i];
                selectHtml+='<option value='+relationshiptype.id+'>'+relationshiptype.name+'</option>';
            }
            selectHtml+='<select></td></tr>';
            html+=selectHtml;
            html+="<tr><td><input type='button' onclick='saveGuardian()' value='Save Guardian'/></td></tr></table>";
            jQuery("#addguardian").html(html);
        }        
    });    
   
}
function saveGuardian(){
    var childid=$("#childid").val();
    var middlename=$("#gaddmiddlename").val();
    var relationship=$("#gaddrelationship").val();
    var firstname=$("#gaddfirstname").val();
    var lastname=$("#gaddlastname").val();
    var phonenumber=$("#gaddphonenumber").val();
    $("#addguardian").html("");
    var url='childupdate/addguardian?firstname='+firstname+'&lastname='+lastname+'&middlename='+middlename+'&childid='+childid+'&relationship='+relationship+'&phonenumber='+phonenumber;
    var guardianHtml='';
    jQuery.get(url,function(data){
        if(data){
            var guardians=data["names"];
            var guardian=guardians[0];
            guardianHtml+="<tr><td>";
            guardianHtml+="<div id='name"+guardian.guardianid+"'>";
            guardianHtml+=guardian.lastname+" "+guardian.firstname+" "+guardian.middlename;
            guardianHtml+="<a href='#' onclick='editGuardianName("+guardian.guardianid+")'>Edit Name</a>";
            guardianHtml+="<div id='editnameerror'></div>";
            guardianHtml+="</div></td>";
            guardianHtml+="<td>"+guardian.relationship+"</td>";                           
            guardianHtml+="<td>"+guardian.phonenumber+"</td>";
            guardianHtml+="</tr>";
            guardianHtml+="<input type='hidden' id='guardianfname"+guardian.guardianid+"' value='"+guardian.firstname+"'/>";
            guardianHtml+="<input type='hidden'  id='guardianlname"+guardian.guardianid+"' value='"+guardian.lastname+"'/>";                             
            guardianHtml+="<input type='hidden' id='guardianmname"+guardian.guardianid+"' value='"+guardian.middlename+"'/>"; 
            var appendHTML=jQuery(guardianHtml);
            appendHTML.appendTo(jQuery('#newrows'));
        }else{
            alert('nod');
        }
    });
}
function addMedicalInformation(){
   
    var html="<form id='medicalinfo'  action='#'><table class='foorm'>";
    html+="<tr><td>Hospital Visited</td><td><input type='text' id='hospitalvisited' name='hospitalvisited' value=''/></td></tr>";
    html+="<tr><td>Ailment</td><td><input type='text' id='ailment' name='ailment' value=''/></td></tr>";
    html+="<tr><td>Cost</td><td><input type='text' id='cost' name='cost' value=''/></td></tr>";
    html+="<tr><td>Doctor Comments</td><td><input type='text' id='doctorcomments' name='doctorcomments' value=''/></td></tr>";
    html+="<tr><td colspan='2'><input type='button' onclick='saveMedicalInformation()' value='Save Medical Information'/></td></tr></table></form>";
    $("#addmedicalinformation").html(html);
}
function saveMedicalInformation(){
    $.get("childupdate/addmedicaldata?"+$("#medicalinfo").serialize()+"&childid="+$("#childid").val(),function(data){
        $("#addmedicalinformation").html("");
    });
}
function test(){
    $.clone();
}
